package com.wfcm.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wfcm.jedis.JedisDao;
import com.wfcm.jedis.JedisResult;
import net.sf.ehcache.search.expression.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wfcm.dao.WfActivityNewsDao;
import com.wfcm.entity.WfActivityNewsEntity;
import com.wfcm.service.WfActivityNewsService;

@Service("wfActivityNewsService")
public class WfActivityNewsServiceImpl implements WfActivityNewsService {
	@Autowired
	private WfActivityNewsDao wfActivityNewsDao;
	@Autowired
	private JedisDao jedisDao;

	/*@Value("${Test_Activity}")
	private String testActivity;*/
	private String testActivity = "Test_Activity";

	@Override
	public WfActivityNewsEntity queryObject(Integer id){
		return wfActivityNewsDao.queryObject(id);
	}
	
	@Override
	public List<WfActivityNewsEntity> queryList(Map<String, Object> map){
		return wfActivityNewsDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return wfActivityNewsDao.queryTotal(map);
	}
	
	@Override
	public void save(WfActivityNewsEntity wfActivityNews){
		wfActivityNewsDao.save(wfActivityNews);
	}
	
	@Override
	public void update(WfActivityNewsEntity wfActivityNews){
		wfActivityNewsDao.update(wfActivityNews);
	}
	
	@Override
	public void delete(Integer id){
		wfActivityNewsDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		wfActivityNewsDao.deleteBatch(ids);
	}

	/**
	 * 根据内容分类查询内容.
	 *
	 * [{src,height:240,width:670,srcB,heightB:240,widthB:550,href,alt}]
	 *
	 * 增加redis缓存访问. 注意: redis是noSQL数据库.是内存数据库.相对稳定性差. 提供服务的时候,要考虑redis宕机的情况.
	 * 如果redis服务宕机,不能影响整体业务流程.
	 *  1. 访问redis,找缓存
	 *  2. 判断缓存查询结果. 有数据直接返回. 如果没有数据执行后续操作
	 *  3. 访问数据库查询内容数据. [已完成]
	 *  4. 将访问数据库的查询结果,保存到redis中.
	 *  5. 返回查询结果.
	 */
	@Override
	public JedisResult getTest(String acId) {
		// 1. 访问redis
		try{
			String redisResult = this.jedisDao.hget(this.testActivity, acId);

			// 2. 判断访问redis结果
			if(redisResult != null && redisResult.trim().length() > 0){
				// 将JSON格式的JedisResult字符串.转化成JedisResult对象. 其中data属性类型为List集合.集合泛型为Map
				return JedisResult.formatToList(redisResult, WfActivityNewsEntity.class);
				// 将JSON格式的JedisResult字符串.转化成JedisResult对象. 其中data属性类型为引用类型,data具体类型是Object
				// JedisResult.formatToPojo(jsonString, Object.class);
			}
		}catch(Exception e){
			e.printStackTrace();
		}

		// 3. 访问数据库
		List<WfActivityNewsEntity> acList = this.wfActivityNewsDao.queryList(acId);

		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		// 整理结果
		for(WfActivityNewsEntity activityNews : acList){
			Map<String, Object> element = new HashMap<String, Object>();

			element.put("title", activityNews.getTitle()); // 图片2
			element.put("subtitle", activityNews.getSubtitle());
			element.put("content", activityNews.getContent());
			element.put("createtime", activityNews.getCreatetime()); // url连接地址

			data.add(element);
		}
		JedisResult resultObject = JedisResult.ok(data);

		// 4. 增加redis缓存
		try{
			ObjectMapper mapper = new ObjectMapper();
			this.jedisDao.hset(this.testActivity, acId,
					mapper.writeValueAsString(resultObject));
		}catch(Exception e){
			e.printStackTrace();
		}

		// 5. 返回结果
		return resultObject;
	}
}
